var express = require('express');
var router = express.Router();
let { Typemodel, Yimodel, Searchmodel } = require('../db/model.js')
/* GET home page. */
router.post('/addType', function (req, res, next) {
	Typemodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '添加成功'
		})
	})
});
router.post('/addYi', function (req, res, next) {
	Yimodel.create(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '添加成功'
		})
	})
});
router.post('/addSearch', function (req, res, next) {
	Searchmodel.insertMany(req.body).then(rep => {
		res.send({
			code: 200,
			msg: '添加成功'
		})
	})
});
router.get('/type', function (req, res, next) {
	Typemodel.find().then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/yi', function (req, res, next) {
	let {y_type,tofb}=req.query
	let search={}
	if(y_type){
		search.y_type=y_type
	}
	Yimodel.find(search).populate('y_type').sort({price:Number(tofb)}).then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
router.get('/search', function (req, res, next) {
	Searchmodel.find().then(rep => {
		res.send({
			code: 200,
			data:rep
		})
	})
});
module.exports = router;
